﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/5/20 星期一 22:59:03
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.Consts;
using Sector7.Framework.EntityType;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// 挂号信息数据访问对象
    /// </summary>
    public class RegistryRecordDAO : DAOBase<M7_RegistryRecord>, IRegistryRecordDAO
    {
        #region Public functions

        /// <summary>
        /// 添加挂号信息
        /// </summary>
        /// <param name="record">挂号信息</param>
        /// <returns></returns>
        public override object Create(M7_RegistryRecord record)
        {
            string sql = @"
                INSERT INTO M7_RegistryRecord
                        (RecordId
                        ,CitizenId
                        ,DeptId
                        ,DoctorId
                        ,InvoiceNumber
                        ,OutpatientType
                        ,RegistryType
                        ,InsuranceType
                        ,OperatorId
                        ,RecordTime
                        ,Amount
                        ,VisitNo
                        ,Status)
                    VALUES
                        (@RecordId
                        ,@CitizenId
                        ,@DeptId
                        ,@DoctorId
                        ,@InvoiceNumber
                        ,@OutpatientType
                        ,@RegistryType
                        ,@InsuranceType
                        ,@OperatorId
                        ,@RecordTime
                        ,@Amount
                        ,@VisitNo
                        ,@Status)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(record.RecordId, "RecordId"),
                    DBUtil.CreateSqlParameter(record.CitizenId, "CitizenId"),
                    DBUtil.CreateSqlParameter(record.DeptId, "DeptId"),
                    DBUtil.CreateSqlParameter(record.DoctorId, "DoctorId"),
                    DBUtil.CreateSqlParameter(record.InvoiceNumber, "InvoiceNumber"),
                    DBUtil.CreateSqlParameter(record.OutpatientType, "OutpatientType"),
                    DBUtil.CreateSqlParameter(record.RegistryType, "RegistryType"),
                    DBUtil.CreateSqlParameter(record.InsuranceType, "InsuranceType"),
                    DBUtil.CreateSqlParameter(record.OperatorId, "OperatorId"),
                    DBUtil.CreateSqlParameter(record.RecordTime, "RecordTime"),
                    DBUtil.CreateSqlParameter(record.Amount, "Amount"),
                    DBUtil.CreateSqlParameter(record.VisitNo, "VisitNo"),
                    DBUtil.CreateSqlParameter(record.Status, "Status"));
            }
        }

        /// <summary>
        /// 获取一天的挂号记录
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public List<M7_RegistryRecord> GetRegistryRecoredListInOneDay(DateTime date)
        {
            string sql = @"SELECT * FROM M7_RegistryRecord rr, S7_Citizen ci
                         WHERE 1 = 1
                         and rr.CitizenId = ci.CitizenId
                         and DATEDIFF(DAY, rr.RecordTime, @RecordTime) = 0 
                         ORDER BY rr.RecordTime DESC ";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_RegistryRecord> registryRecordList = new List<M7_RegistryRecord>();

                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(date, "RecordTime"));
                while (reader.Read())
                {
                    M7_RegistryRecord registryRecord = new M7_RegistryRecord();
                    DBUtil.ReadData(reader, registryRecord);

                    S7_Citizen citizen = new S7_Citizen();
                    DBUtil.ReadData(reader, citizen);
                    registryRecord.Patient = citizen;

                    registryRecordList.Add(registryRecord as M7_RegistryRecord);
                }

                return registryRecordList;
            }
        }

        #endregion

        #region Structure
        public RegistryRecordDAO()
        {
            m_tableName = C_M7_TableName.REGISTRY_RECORD;
            m_primaryKeyName = C_PrimaryKeyName.REGISTRY_RECORD_ID;
        }
        #endregion
    }
}
